

Imports System.Configuration.Provider
Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.Specialized
Imports System.IO
Imports System.Linq
Imports Microsoft.DPE.Samples.PictureProvider

Namespace Mocks
	Public Class MockPictureProvider
		Inherits Microsoft.DPE.Samples.PictureProvider.PictureProvider
		Private _imagesPath As String
		Public ReadOnly Property ImagesPath() As String
			Get
				Return _imagesPath
			End Get
		End Property

		Public Overloads Overrides Sub Initialize(name As String, config As NameValueCollection)
			MyBase.Initialize(name, config)

            Dim s As String = config.Get("imagesPath")
			If s Is Nothing Then
				Throw (New ProviderException("imagesPath is not set"))
			End If

			If Not Directory.Exists(s) Then
				Throw (New ProviderException("imagesPath directory does not exists"))
			End If

			Dim info As New DirectoryInfo(s)
			_imagesPath = info.FullName
		End Sub

		Public Overloads Overrides Function GetPictureById(pictureId As String) As Picture
			Dim list As List(Of Picture) = GetPictures()
            Dim dict As Dictionary(Of String, Picture) = list.ToDictionary(Function(k) (k.Id))

			If Not dict.ContainsKey(pictureId) Then
				Return Nothing
			End If

			Dim p As Picture = dict(pictureId)

			Return p
		End Function

		Public Overloads Overrides Function GetPictures() As List(Of Picture)
			Dim files As String() = Directory.GetFiles(ImagesPath)

			Dim list As New List(Of Picture)()

			PopulatePictureListFromFiles(list, files)

			Return (list)
		End Function

		Public Overloads Overrides Function GetPicturesByTag(tagName As String) As List(Of Picture)
			Return (GetPictures())
		End Function

		Private Sub PopulatePictureListFromFiles(list As List(Of Picture), files As String())
			Dim counter As Integer = 1
			For Each file As String In files
				Dim info As New FileInfo(file)

				Dim p As New Picture()

				p.Id = info.FullName
				p.Title = "Item #" & counter
				p.Tags = New String() {"Tag1", "Tag2", "Tag3"}
				p.ItemUrl = New Uri(file)
				p.FileName = info.Name

				list.Add(p)

				counter += 1
			Next
		End Sub
	End Class
End Namespace
